package com.one.rope.mvp.basecore.bean;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.one.rope.mvp.basecore.exception.BaseException;
import com.one.rope.mvp.basecore.util.StringUtils;
import io.swagger.annotations.ApiModel;

/**
 * @author Weijian.liu
 * @desc 接口返回对象
 * @date 2018/11/26
 */
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseObject<T> extends ResponseGeneric<Object> {

  public ResponseObject() {
    super();
  }

  public ResponseObject(Object data) {
    super(data);
  }

  public ResponseObject(ResponseMessage responseMessage) {
    super(responseMessage);
  }

  public ResponseObject(int code, String message) {
    super(code, message);
  }

  public ResponseObject(ResponseMessage responseMessage, Object data) {
    super(responseMessage, data);
  }

  public ResponseObject(int code, String message, Object data) {
    super(code, message, data);
  }

  public static ResponseObject systemError(Throwable e) {
    return systemError(e, null);
  }

  public static ResponseObject userError(BaseException e) {
    return new ResponseObject(e.getCode(), e.getMsg());
  }

  public static ResponseObject systemError(Throwable e, String prefix) {
    try {
      if (e instanceof BaseException) {
        BaseException exception = (BaseException) e;
        return userError(exception);
      }
      prefix = StringUtils.isNullOrEmpty(prefix) ? "" : prefix + ":";
      ResponseMessage message = ResponseMessage.systemError(e);
      return ResponseObject.systemError(prefix + message.getMsg());
    } catch (Exception e2) {
      return ResponseObject.systemError(e2.toString());
    }
  }

  public static ResponseObject systemError(String msg) {
    return new ResponseObject(new ResponseMessage(BaseBusinessMessage.FAILURE_CODE, msg));
  }

  public static ResponseObject successObject() {
    return new ResponseObject();
  }

  public static ResponseObject successObject(Object object) {
    return new ResponseObject(object);
  }

  public static ResponseObject userFailure(String msg) {
    ResponseMessage message = ResponseMessage.userFailure(msg);
    return new ResponseObject(message);
  }

  public static ResponseObject userFailure(String msg, Object data) {
    ResponseMessage message = ResponseMessage.userFailure(msg);
    return new ResponseObject(message, data);
  }

  public static ResponseObject userFailure(ResponseMessage message) {
    return new ResponseObject(message);
  }

  public static ResponseObject userFailure(ResponseMessage message, Object data) {
    return new ResponseObject(message, data);
  }

  public void setUserFailure(ResponseMessage message) {
    this.code = message.getCode();
    this.msg = message.getMsg();
  }

}
